/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.hackystat.devcathlon.engine.team;

import java.util.List;
import org.hackystat.devcathlon.engine.test.DevcathlonTestHelper;
import org.junit.After;
import org.junit.Before;
import static org.junit.Assert.*;
import org.junit.Test;

/**
 * Test TeamManager class.
 * 
 * @author Anthony Du
 */
public class TestTeamManager extends DevcathlonTestHelper {
  private static final String teamName = "TestTeam";
  private static final String teamName2 = "TestTeam2";
  private static final String owner = "admin";
  private static final String owner2 = "admin2";
  private static final String domain = "@hackystat.org";
  private static final String user = owner + domain;
  private Team team1;

  /**
   * Setup team.
   * @throws Exception when an error occurs
   */
  @Before
  public void setUp() throws Exception {
    team1 = makeTeam(teamName, owner, "member1", "member2");
    makeTeam(teamName2, owner2, "member3", "member4");
  }

  /**
   * Cleanup team(s).
   * @throws Exception when an error occurs
   */
  @After
  public void cleanUp() throws Exception {
    deleteTeam(teamName);
    deleteTeam(teamName2);
  }

  /**
   * Test CRUD for Teams management.
   * @throws Exception for any errors that may occur.
   */
  @Test
  public void testTeamManager() throws Exception {
    assertEquals("should be TestTeam", teamName, team1.getName());
    assertEquals("should be owner's email", user, team1.getOwner().getEmail());
  }

  /**
   * Test getAllTeams.
   * @throws Exception when an error occurs.
   */
  @SuppressWarnings("unchecked")
  @Test
  public void testGetAllTeams() throws Exception {
    List<Team> teams = (List<Team>) TeamManager.getInstance().getAllTeams();
    assertFalse("should have teams", teams.isEmpty());
    assertEquals("should have 2 teams", 2, teams.size());
    assertEquals("should be team name: TestTeam", teamName, teams.get(0).getName());
    assertEquals("should be team name: TestTeam2", teamName2, teams.get(1).getName());
    assertEquals("should be 2 TestTeam members", 2, teams.get(0).getMembers().size());
  }
  
  /**
   * Test team project association.
   * @throws Exception when an error occurs.
   */
  @Test
  public void testTeamProject() throws Exception {
    // modify team project name
    String newName = "Project 1";
    team1.getTeamProject().setName(newName);
    // update team
    TeamManager.getInstance().updateTeam(team1);
    // retrieve updated team
    Team updatedTeam = TeamManager.getInstance().getTeam(teamName);
    // test to see if project has been updated
    assertEquals("should be the same project name", 
        team1.getTeamProject().getName(), updatedTeam.getTeamProject().getName());
  }
  
  /**
   * Test get all teams associated to a user.
   * 
   * @throws Exception if an error occurs.
   */
  @Test
  public void testGetTeams() throws Exception {
    List<Team> teams = TeamManager.getInstance().getTeams(user);
    
    assertEquals("should have only 1 team", 1, teams.size());
  }
}
